package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseOilAddDTO;
import com.kaili.basic.pojo.dto.BaseOilQueryDTO;
import com.kaili.basic.pojo.entity.BaseGasStation;
import com.kaili.basic.pojo.entity.BaseOil;
import com.kaili.basic.service.IBaseGasStationService;
import com.kaili.basic.service.IBaseOilService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Validated
@Api(tags = "加油记录管理", description = "加油记录管理相关API")
public class BaseOilController {

    @Autowired
    private IBaseOilService baseOilService;

    @Autowired
    private IBaseGasStationService baseGasStationService;

    @PostMapping("/oil")
    @ApiOperation(value = "新增加油记录")
    public RestStatus addBaseOil(@Validated @RequestBody BaseOilAddDTO oilAddDTO){
        BaseOil oil = new BaseOil();
        BeanUtils.copyProperties(oilAddDTO, oil);
        Long createBy = LoginDetailUtils.getCurrentEmployeeId().longValue();
        oil.setCreateBy(createBy);
        oil.setCreateTime(new Date());
        baseOilService.addBaseOil(oil);
        return CommonStatus.ADD_OK;
    }

    @PutMapping("/oil")
    @ApiOperation(value = "修改加油记录")
    public RestStatus updateBaseOil(@Validated @RequestBody BaseOilAddDTO oilAddDTO){
        BaseOil oil = new BaseOil();
        BeanUtils.copyProperties(oilAddDTO, oil);
        oil.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        oil.setModifyTime(new Date());
        baseOilService.updateBaseOil(oil);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/oil/{oilId}")
    @ApiOperation(value = "删除加油记录")
    public RestStatus deleteBaseOil(@PathVariable("oilId") Long oilId){
        BaseOil oil = new BaseOil();
        oil.setOilId(oilId);
        oil.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        oil.setModifyTime(new Date());
        baseOilService.deleteBaseOil(oil);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping("/oils")
    @ApiOperation(value = "根据查询条件查询加油记录")
    public Page<BaseOil> getBaseOilByKeywords(@ModelAttribute BaseOilQueryDTO oilQueryDTO){
        BaseOil oil = new BaseOil();
        BeanUtils.copyProperties(oilQueryDTO, oil);
        if(LoginDetailUtils.getSiteId()==1)
            oil.setSignSiteId(null);
        return baseOilService.getBaseOilByKeywords(new Page<BaseOil>(oil.getPage(),oil.getPageSize()),oil);
    }

    @GetMapping("/oils/{stationName}")
    @ApiOperation(value = "根据查询参数模糊查询加油站")
    public List<BaseGasStation> getBaseGasStationByKeywords( @PathVariable("stationName") String stationName){
        return baseGasStationService.getBaseGasStationByKeywords(stationName);
    }

}

